﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class findin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }

        string JudgePower = Session["power"].ToString();
        if (JudgePower != "超级管理员" && JudgePower != "仓库管理员" && JudgePower != "销售" && JudgePower != "查询用户" && JudgePower != "工程师" && JudgePower != "主管")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
        if (!IsPostBack)
        {
            this.bind();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["DID"] = GridView1.SelectedValue.ToString();
        Response.Redirect("~/findin1.aspx?");
    }
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    protected void bind()
    {
        Label3.Text = "全部";
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr;
        sqlStr = "select ID as 编号 , DName as 购货单名称,DDes as 类别,PName as 所属项目,DXiao as 采购人,DHetong as 合同号,beizhu as 备注 from [Projects],[Dan] where  Dan.DPID=Projects.PID and DDes='购货单' order by ID desc";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataKeyNames = new string[] { "编号" };
        GridView1.DataBind();
        myDa.Dispose();
        myDs.Dispose();

        string sql1 = "select (bianhao+PName) as Pname from Projects order by bianhao desc";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql1, myConn);
        DataSet sqlDs = new DataSet();
        sqlDa.Fill(sqlDs);
        DropDownList1.DataSource = sqlDs;
        DropDownList1.DataTextField = "Pname";
        DropDownList1.DataBind();
        this.DropDownList1.Items.Insert(0, new ListItem("查看全部", "查看全部"));
        sqlDa.Dispose();
        sqlDs.Dispose();

        myConn.Close();
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "查看全部")
        {
            this.bind();
        }
        else
        {
            Label3.Text = "按项目查询";
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlStr;
            sqlStr = "select ID as 编号 , DName as 购货单名称,DDes as 类别,PName as 所属项目,DXiao as 采购人,DHetong as 合同号,beizhu as 备注 from [Projects],[Dan] where  Dan.DPID=Projects.PID and DDes='购货单' and bianhao+''+PName like '%" + DropDownList1.SelectedValue + "%'  order by ID desc";
            SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
            DataSet myDs = new DataSet();
            myDa.Fill(myDs);
            GridView1.DataSource = myDs;
            GridView1.DataKeyNames = new string[] { "编号" };
            GridView1.DataBind();
            myDa.Dispose();
            myDs.Dispose();
            myConn.Close();
        }

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        if (Label3.Text == "全部")
        {
            bind();
        }

        else if (Label3.Text == "按项目查询")
            Button6_Click(sender, e);

    }
}